<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
        </el-breadcrumb>

        <h3 style="margin: 20px 0;">品牌管理</h3>

        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" width="150" align="center"></el-table-column>
            <el-table-column prop="pinyin" label="拼音" width="180" align="center"></el-table-column>
            <el-table-column prop="keywords" label="关键词" header-align="center"></el-table-column>
            <el-table-column prop="productCount" label="商品数量" width="80" align="center"></el-table-column>
            <el-table-column prop="sales" label="销量" width="80" align="center"></el-table-column>
            <el-table-column label="好评 / 评论数量" width="150" align="center">
                <template slot-scope="scope">
                    <span v-text="scope.row.positiveCommentCount"></span> / <span v-text="scope.row.commentCount"></span>
                </template>
            </el-table-column>
            <el-table-column prop="enable" label="是否启用" width="90" align="center">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.enable"
                            @change="handleChangeEnable(scope.$index, scope.row.id, scope.row.enable)"
                            :active-value="1"
                            :inactive-value="0"
                            active-color="#13ce66"
                            inactive-color="#999">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
                <template slot-scope="scope">
                    <el-button circle icon="el-icon-edit" size="mini" @click="handleEdit(scope.row.id)"></el-button>
                    <el-button circle icon="el-icon-delete" size="mini" type="danger"
                               @click="openDeleteConfirm(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: []
            }
        },
        methods: {
            openDeleteConfirm(id) {
                this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleDelete(id);
                }).catch(() => {
                });
            },
            handleEdit(id) {
                console.log('将编辑id=' + id + "的品牌数据……");
            },
            handleDelete(id) {
                console.log('将删除id=' + id + "的品牌数据……");
                let url = 'http://localhost:9080/brands/' + id + '/delete';
                this.axios
                    .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                    .post(url).then((response) => {
                    let json = response.data;
                    if (json.code == 20000) {
                        this.$message({
                            message: '删除品牌成功！',
                            type: 'success'
                        });
                    } else {
                        this.$message.error(response.data.message);
                    }
                    this.loadBrands();
                })
            },
            handleChangeEnable(i, id, toState) {
                let enableText = ['禁用', '启用'];
                let originState = (toState + 1) % 2;
                this.$message.error('尝试将id=' + id + '的品牌状态从【' + enableText[originState] + '】改为【' + enableText[toState] + '】，但是，此功能尚未实现！');
                setTimeout(() => {
                    this.tableData[i].enable = originState;
                }, 200);
            },
            loadBrands: function () {
                console.log('loadBrands()');
                let url = 'http://localhost:9080/brands';
                console.log('url = ' + url);
                this.axios
                    .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                    .get(url).then((response) => {
                    let json = response.data;
                    if (json.code == 20000) {
                        // let list = json.data;
                        // for (let i = 0; i < list.length; i++) {
                        //   console.log(list[i]);
                        // }
                        this.tableData = json.data;
                    } else {
                        this.$message.error(response.data.message);
                    }
                });
            }
        },
        created() {
            // created >>> 已创建，将在mounted、显示页面之前执行
            console.log('vue created');
        },
        mounted() {
            // mounted >>> 已挂载，将在created之后、显示页面之前执行
            console.log('vue mounted');
            this.loadBrands();
        }
    }
</script>

<style scoped>

</style>